package henu.graduation_design.onlineshop;

import henu.graduation_design.util.JsonLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;

public class AddressActivity extends Activity {

	EditText namEditText, addressEditText, phoneEditText, zcodeEditText;
	Button submitBtn;
	SharedPreferences sp;
	private JsonLoader jsonLoader = new JsonLoader();
	
	private void loadJson(final String url, SharedPreferences spp,
			final Map<String,Object> mapEntry) {
		jsonLoader.postJson(url, spp, mapEntry,
				new JsonLoader.JsonObjectCallback() {
					public void jsonLoaded(JSONObject jsonObject) {
						boolean result = false;
						try {
							result = jsonObject
									.getBoolean("createOrderResult");
						} catch (JSONException e) {
							e.printStackTrace();
						}
						if (result) {
							Toast.makeText(getApplicationContext(),
									"购买成功，请等待发货", Toast.LENGTH_LONG).show();
							finish();
						} else {
							Toast.makeText(getApplicationContext(), "购买失败",
									Toast.LENGTH_LONG).show();
						}
					}
				});
	}                                  

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_address);
		Intent intent = getIntent();
		final ArrayList<Integer> iDList = intent
				.getIntegerArrayListExtra("IDList");
		namEditText = (EditText) findViewById(R.id.address_name);
		phoneEditText = (EditText) findViewById(R.id.address_phone);
		addressEditText = (EditText) findViewById(R.id.address_address);
		zcodeEditText = (EditText) findViewById(R.id.address_zcode);
		submitBtn = (Button) findViewById(R.id.address_submit);

		submitBtn.setOnClickListener(new OnClickListener() {
			@SuppressLint("ShowToast")
			public void onClick(View v) {
				if (namEditText.getText() == null
						|| namEditText.getText().toString() == ""
						|| phoneEditText.getText() == null
						|| phoneEditText.getText().toString() == ""
						|| addressEditText.getText() == null
						|| addressEditText.getText().toString() == ""
						|| zcodeEditText.getText() == null
						|| zcodeEditText.getText().toString() == "") {
					Toast.makeText(getApplicationContext(), "请将信息填写完整",
							Toast.LENGTH_LONG).show();
				} else {
					JSONArray array = new JSONArray(iDList);
					Map<String,Object> mapEntry=new HashMap<String, Object>();
				    mapEntry.put("name", namEditText.getText().toString());
				    mapEntry.put("address", addressEditText.getText()
							.toString());
				    mapEntry.put("zcode", zcodeEditText.getText().toString());
				    mapEntry.put("phone", phoneEditText.getText().toString());
				    mapEntry.put("productIDList", array.toString());
					sp = getSharedPreferences("SP", MODE_PRIVATE);
					if (!sp.getBoolean("IsLogin", false)) {
						Toast.makeText(getApplicationContext(), "登陆超时", Toast.LENGTH_LONG);
					} else {
						try {
							loadJson(getString(R.string.url_create_order),sp,
									mapEntry);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

				}
			}
		});
	}
}
